﻿using System;
using System.IO;

namespace easysmb
{
    using easysmbrary;

    public class EasySMBFile
    {
        public easysmbrary.FileAttributes Attributes { get; internal set; }
        public DateTime? LastEditTime { get; internal set; }
        public DateTime? CreationTime { get; internal set; }
        public long FileSize { get; internal set; }
        internal string path { get; set; }
        internal EasySMB smb { get; set; }
        public bool Hiden { get; internal set; }
        public string FullPath { get; internal set; }
        public string FileExtension { get; internal set; }
        public string FileName { get; internal set; }
        internal EasySMBFile(EasySMB smb, string path, FileNetworkOpenInformation info)
        {
            this.smb = smb;
            this.path = path;
            this.FullPath = path;
            this.FileName = smb.read_name(path);
            this.FileExtension = smb.read_ext(path);
            this.Attributes = info.FileAttributes;
            this.LastEditTime = info.LastWriteTime;
            this.CreationTime = info.CreationTime;
            this.FileSize = info.EndOfFile;
            this.Hiden = (info.FileAttributes & easysmbrary.FileAttributes.Hidden) != 0;
        }
        internal EasySMBFile(EasySMB smb, string path, FileDirectoryInformation info)
        {
            this.smb = smb;
            this.path = path;
            this.FullPath = path;
            this.FileName = smb.read_name(path);
            this.FileExtension = smb.read_ext(path);
            this.Attributes = info.FileAttributes;
            this.LastEditTime = info.LastWriteTime;
            this.CreationTime = info.CreationTime;
            this.FileSize = info.EndOfFile;
        }
        public EasySMBFileStream GetStream()
        {
            var stream = new EasySMBFileStream(this);

            stream.load_filehandle(FileAccess.ReadWrite, FileShare.Read);
            return stream;
        }
        public EasySMBFileStream GetStream(FileAccess access, FileShare share)
        {
            var stream = new EasySMBFileStream(this);

            stream.load_filehandle(access, share);
            return stream;
        }
        public void Rename(string newname)
        {
            if (smb.read_name(path) == newname) return;
            smb.Rename(this.path, newname);
            this.path = smb.change_name(this.path, newname);
        }
        public void Delete()
        {
            smb.DeleteFile(this.path);
        }
    }
}
